package com.aliyun.edu.advanced.development.thread.achieve;

/**
 * @author xiaoming
 * @version 1.0
 * @date 2019-02-22 10:34
 **/
public class RunnableAndThread {
    public static void main(String[] args) {
        // 区别
        // 1. Runnable 避免单继承的局限
        // 2. Runnable 可以更好的描述数据共享的概念;

        MyThread3  mt1 = new MyThread3("线程A");
        MyThread3  mt2 = new MyThread3("线程B");
        MyThread3  mt3 = new MyThread3("线程C");
        // Thread 数据共享
//        new Thread(mt1).start();
//        new Thread(mt2).start();
//        new Thread(mt3).start();

        // 各卖各的票
//        new MyThread3("线程D").start();
//        new MyThread3("线程E").start();

        // Runnable 的数据共享
        MyThread4 mt4 = new MyThread4("线程F");

        //没有start()   mt4.start()
        new Thread(mt4).start();
        new Thread(mt4).start();
        new Thread(mt4).start();

    }
}

class MyThread3 extends Thread {
    private String title;
    public MyThread3(String title) {
        this.title = title;
    }
    // 票
    private int ticket = 10;
    @Override
    public void run() {
        for (int i = 0; i < ticket; i++) {
            System.out.println( this.title + "卖票: " + this.ticket --);
        }
    }
}

class MyThread4 implements Runnable {
    private String title;
    public MyThread4(String title) {
        this.title = title;
    }
    // 票
    private int ticket = 10;
    @Override
    public void run() {
        for (int i = 0; i < ticket; i++) {
            System.out.println( this.title + "卖票: " + this.ticket --);
        }
    }
}

